<?php

namespace app\screen\controller\tencent;

use app\common\controller\Screen;

use hg\apidoc\annotation as Apidoc;
use think\facade\Env;

/**
 * @Apidoc\Title("地图相关服务接口")
 */
class Service extends Screen
{

    /**
     * @Apidoc\Title("路线规划")
     * @Apidoc\Url("/screen/tencent.Service/driving")
     * @Apidoc\Param("from",type="string",require=true, desc="起点坐标")
     * @Apidoc\Param("to",type="string",require=true, desc="终点坐标")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function driving()
    {
        try {
            $param = $this->request->param();
            Validate([
                'from|起点坐标' => 'require',
                'to|终点坐标' => 'require',
            ])->check($param);
            $param['key'] =  Env::get('TencentCloud.map');
            $url = "https://apis.map.qq.com/ws/direction/v1/driving/?" . http_build_query($param);
            $res = httpGet($url);
            $res = json_decode($res, true);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, "成功", $res);
    }

    /**
     * @Apidoc\Title("地点搜索")
     * @Apidoc\Url("/screen/tencent.Service/search")
     * @Apidoc\Param("keyword",type="string",require=true, desc="搜索关键字")
     * @Apidoc\Method("POST")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     */
    public function search()
    {
        try {
            $param = $this->request->param();
            Validate([
                'keyword|搜索关键字' => 'require',
            ])->check($param);
            $param['key'] = 'YSLBZ-R6MKL-U2APE-EDWOL-FQZEO-6QFP4';
            $url = "https://apis.map.qq.com/ws/place/v1/search?" . http_build_query($param);
            $res = httpGet($url);
            $res = json_decode($res, true);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, "成功", $res);
    }


}